﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2010, hrondra
 */

using System;
using System.Collections.Generic;

namespace Close.Data
{

    public partial class GameData
    {
        partial class ImprintedDataTable
        {
            public byte getvalue(ushort gid, int radek, int sloupec)
            {
                return (byte)((string)this.FindBygid(gid)[this.valuesColumn.ColumnName])[radek * 9 + sloupec];
            }

            /// <summary>
            /// Delete imprinted rows for level id.
            /// </summary>
            /// <param name="lid">level id</param>
            /// <returns>All games id for lid.</returns>
            public List<ushort> DeleteRows(byte lid)
            {
                System.Collections.Generic.List<UInt16> keys = new List<ushort>();
                foreach (ImprintedRow row in this.Select(String.Format("{0}={1}", this.lidColumn.ColumnName, lid)))
                    keys.Add(row.gid);
                foreach (UInt16 key in keys)
                {
                    this.FindBygid(key).Delete();
                }
                return keys;
            }
        }
        partial class SavedGameDataTable
        {

            public byte getvalue(ushort gid, int radek, int sloupec)
            {
                byte val = (byte)((string)this.FindBygid(gid)[this.valuesColumn.ColumnName])[radek * 9 + sloupec];
                return val;

            }
        }
        public partial class SavedGameRow
        {
            public char getValue(int radek, int sloupec)
            {
                return this.values[radek * 9 + sloupec];
            }

            /// <summary>
            /// Get notes in uint16 representation
            /// </summary>
            /// <param name="radek">row</param>
            /// <param name="sloupec">colums</param>
            public short GetNotes(int radek, int sloupec)
            {
                return BitConverter.ToInt16(this.notes, radek * 9 * 2 + sloupec * 2);

            }

            /// <summary>
            /// Set notes,
            /// </summary>
            /// <param name="notesInInt">notes in int16</param>
            public void SetNotes(short[] notesInInt)
            {
                byte[] nts = new byte[162];
                for (int ijk = 0; ijk < 81; ijk++)
                {
                    BitConverter.GetBytes(notesInInt[ijk]).CopyTo(nts, ijk * 2);
                }
                this.notes = nts;
            }
        }
        /// <summary>
        /// First free id.
        /// </summary>
        public byte GetMaxGameLevel()
        {
            if (Level.Rows.Count == 0)
                return 3;
            else
                return (byte)Level.Compute(String.Format("Max({0})", Level.lidColumn.ColumnName), string.Empty);
        }

        /// <summary>
        /// First free id.
        /// </summary>
        public ushort GetMaxGameId()
        {
            if (Imprinted.Rows.Count == 0)
                return 90;
            else
                return (ushort)Imprinted.Compute(String.Format("Max({0})", Imprinted.gidColumn.ColumnName), string.Empty);
        }
    }
}
